package LeetcodeTest_thirty_four;
//24. 两两交换链表中的节点

import java.util.Scanner;

public class swapPairs
{
    public static void main(String[] args)
    {
        ListNode head=Solutions.createListNode();
        head=Solutions.swapPairs(head);
        Solutions.Print(head);
    }
}
class Solutions
{
    public static ListNode swapPairs(ListNode head)
    {
        if(head==null||head.next==null)
            return head;
        else
        {
            ListNode p1,p2;
            p1=head;
            p2=head.next;
            p1.next=p2.next;
            p2.next=p1;
            head=p2;
            p2=p1.next;
            while(p2!=null&&p2.next!=null)
            {
                p1.next=p2.next;
                p1=p1.next;
                p2.next=p1.next;
                p1.next=p2;
                p1=p1.next;
                p2=p2.next;
            }
        }
        return head;
    }
    public static ListNode createListNode()
    {
        int a;
        Scanner reader=new Scanner(System.in);
        ListNode head=null;
        ListNode tail=null;
        head=new ListNode(0);
        tail=head;
        a=reader.nextInt();
        while(a!=-1)
        {
            ListNode p=new ListNode(a);
            tail.next=p;
            tail=tail.next;
            a=reader.nextInt();
        }
        return head.next;
    }
    public static void Print(ListNode head)
    {
        ListNode p=head;
        while(p!=null)
        {
            System.out.print(p.val+"   ");
            p=p.next;
        }
    }
}